package cn.edu.hpu.jie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.hpu.jie.entity.Active;
import cn.edu.hpu.jie.util.RowMapping;
import cn.edu.hpu.jie.util.VoteDB;

public class ActiveDao {
	private VoteDB<Active> db = new VoteDB<Active>();
	
	private class ActiveMapping implements RowMapping<Active>{

		public Active getMapping(ResultSet rs) throws SQLException {
			Active a = new Active();
			a.setId(rs.getInt("id"));
			a.setTitle(rs.getString("title"));
			a.setSites(rs.getString("sites"));
			a.setType(rs.getInt("type"));
			a.setContent(rs.getString("content"));
			return a;
		}
	}
	
	public Active getActiveByType(int type) {
		String sql = "SELECT * FROM t_active WHERE type = ?";
		return db.getObject(sql, new ActiveMapping(), type);
	}

	public int insertActive(Active a) {
		String sql = "INSERT INTO t_active (title, type, content, sites) VALUES (?, ?, ?, ?)";
		return db.getInsertPrimaryKey(sql, a.getTitle(), a.getType(), a.getContent(), a.getSites());
	}

	public void updateActiveByType(Active active) {
		String sql = "UPDATE t_active SET title = ?, content = ?, sites = ? WHERE type = ?";
		db.updateData(sql, active.getTitle(), active.getContent(), active.getSites(), active.getType());
	}
}
